﻿using System;
using System.Globalization;
using System.Threading;
using CAION2.Classes;
using CAION2.Helpers;
using CAION2.Properties;
using FirstFloor.ModernUI.Presentation;

namespace CAION2.ViewModels
{
    internal class MainViewModel : ViewModelBase
    {
        #region Vars

        private string _title;
        private LinkGroupCollection _menuLinkGroups;
        
        #endregion

        #region Properties

        public string Title
        {
            get
            {
                return _title;
            }
            private set
            {
                if (Equals(_title, value)) return;
                _title = value;

                OnPropertyChanged("Title");
            }
        }
        
        public string ReceptureTitle { get; set; }

        public LinkGroupCollection MenuLinkGroups
        {
            get
            {
                return _menuLinkGroups;
            }
            private set
            {
                if (Equals(_menuLinkGroups, value)) return;
                _menuLinkGroups = value;

                OnPropertyChanged("MenuLinkGroups");
            }
        }

        #endregion

        #region Commands

        #endregion

        internal MainViewModel()
        {
            Thread.CurrentThread.CurrentUICulture
                = CultureInfo.GetCultureInfo(ClassConfiguration.GetValue("InterfaceLanguage").ToString());

            Title = Resources.Title;
            ReceptureTitle = Resources.RR_ReceptureTitle;

            string themeColor = ClassConfiguration.GetValue("ThemeColor").ToString();
            switch (themeColor)
            {
                case "Dark":
                    AppearanceManager.Current.ThemeSource = AppearanceManager.DarkThemeSource;
                    break;
                case "Light":
                    AppearanceManager.Current.ThemeSource = AppearanceManager.LightThemeSource;
                    break;
                default:
                    AppearanceManager.Current.ThemeSource = new Uri(themeColor, UriKind.RelativeOrAbsolute);
                    break;
            }

            string fontSize = ClassConfiguration.GetValue("FontSize").ToString();
            switch (fontSize)
            {
                case "Large":
                    AppearanceManager.Current.FontSize = FontSize.Large;
                    break;
                case "Small":
                    AppearanceManager.Current.FontSize = FontSize.Small;
                    break;
            }

            int colorIndex = (int) ClassConfiguration.GetValue("AccentColor");
            AppearanceManager.Current.AccentColor = SettingsViewModel.AccentColors[colorIndex];

            Mediator.Instance.Register(o => UpdateReceptureTitle((string)o), Messages.ReceptureTitleChange);
            
            CreateMenu();
        }

        private void UpdateReceptureTitle(string recipeName)
        {
            MenuLinkGroups[1].Links[1].DisplayName = String.Format("{0} \"{1}\"", Resources.RR_ReceptureTitle, recipeName);
        }

        private void CreateMenu()
        {
            LinkGroup characterGroup = new LinkGroup { DisplayName = Resources.CS_Title};
            LinkGroup recipesGroup = new LinkGroup { DisplayName = Resources.RR_Title};

            Link statusLink = new Link { DisplayName = Resources.CS_CharacterTitle, Source = new Uri("/Views/Status_Page.xaml", UriKind.Relative) };
            characterGroup.Links.Add(statusLink);
            Link stigmaLink = new Link { DisplayName = Resources.CS_StigmasTitle, Source = new Uri("/Views/Stigmas_Page.xaml", UriKind.Relative) };
            characterGroup.Links.Add(stigmaLink);

            Link recipesLink = new Link { DisplayName = Resources.RR_RecipesTitle, Source = new Uri("/Views/RecipesList_Page.xaml", UriKind.Relative) };
            recipesGroup.Links.Add(recipesLink);
            Link receptureLink = new Link { DisplayName = Resources.RR_ReceptureTitle, Source = new Uri("/Views/Recepture_Page.xaml", UriKind.Relative) };
            recipesGroup.Links.Add(receptureLink);

            MenuLinkGroups = new LinkGroupCollection {characterGroup, recipesGroup};
        }
    }
}
